/**
* @Title: Demo1.java
* @Package me.shibor.study.javaSE.database
* @Description: TODO
* @author shibor
* @date 2017年2月9日 下午8:52:04
* @version V1.0
*/
package me.shibor.study.javaSE.database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @ClassName: Demo1
 * @Description: 以下实例使用了 JDBC 连接 MySQL 数据库
 * @author shibor
 * @date 2017年2月9日 下午8:52:04
 * 
 */
public class Demo1 {

	public static void main(String[] args) {

		Statement stmt = null;
		ResultSet rs = null;
		String sql = "SELECT * FROM student";
		Connection conn = MysqlConnecter.getConnection();

		try {
			// 执行查询
			System.out.println(" 实例化Statement对...");
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rs = stmt.executeQuery(sql);
			// 展开结果集数据库
			while (rs.next()) {
				// 通过字段检索
				int id = rs.getInt("id");
				String name = rs.getString("name");
				String address = rs.getString("address");

				// 输出数据
				System.out.print("id: " + id);
				System.out.print(", name: " + name);
				System.out.print(", address: " + address);
				System.out.print("\n");
			}
			// 完成后关闭
			rs.close();
			stmt.close();
			conn.close();

		} catch (SQLException e) {
			e.printStackTrace();
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}

		System.out.println("Goodbye!");
	}
}
